/***************************************************************************
 * Copyright 2012-2013 TXT e-solutions SpA
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the contract ICT-257367.
 *
 * Authors:
 *      Salvatore Piccione (TXT e-solutions SpA)
 *
 * Contributors:
 *        Domenico Rotondi (TXT e-solutions SpA)
 **************************************************************************/
package it.txt.ens.client.publisher.example.osgi;

import it.txt.ens.core.factory.ENSResourceFactory;

import java.util.Dictionary;
import java.util.Map;

import org.osgi.framework.Filter;
import org.osgi.framework.ServiceReference;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;

/**
 * @author Salvatore Piccione (TXT e-solutions SpA - salvatore.piccione AT txtgroup.com)
 * @author Domenico Rotondi (TXT e-solutions SpA - domenico.rotondi AT txtgroup.com)
 *
 */
class PublisherFilter implements Filter {

    private String unescapedImplementationID;
    private String unescapedPattern;
    private String unescapedNamespace;
    private long bundleOwnerID;
    private String encodedOsgiFilter;
    
    public PublisherFilter (String unescapedImplementationID, long bundleOwnerID, 
            String unescapedNamespace, String unescapedPattern) {
        if (unescapedImplementationID == null)
            throw new IllegalArgumentException ("The implementation ID cannot be null");
        if (unescapedNamespace == null)
            throw new IllegalArgumentException ("The namespace ID cannot be null");
        if (unescapedPattern == null)
            throw new IllegalArgumentException ("The pattern cannot be null");
        this.unescapedImplementationID = unescapedImplementationID;
        this.unescapedPattern = unescapedPattern;
        this.unescapedNamespace = unescapedNamespace;
        this.bundleOwnerID = bundleOwnerID;
        
        EqualsFilter implementationFilter = new EqualsFilter(
              it.txt.ens.client.publisher.osgi.FilterAttributes.PUBLISHER_IMPLEMENTATION_KEY, 
              unescapedImplementationID);
        EqualsFilter ownershipFilter = new EqualsFilter(
              it.txt.ens.client.publisher.osgi.FilterAttributes.PUBLISHER_OWNER_KEY, bundleOwnerID + "");
        AndFilter filter = new AndFilter().and(implementationFilter).and(ownershipFilter);
        encodedOsgiFilter = filter.encode();
    }

    /* (non-Javadoc)
     * @see org.osgi.framework.Filter#match(org.osgi.framework.ServiceReference)
     */
    @Override
    public boolean match(ServiceReference<?> reference) {
        return matches(reference.getProperty(
                it.txt.ens.client.publisher.osgi.FilterAttributes.PUBLISHER_IMPLEMENTATION_KEY),
            reference.getProperty(ENSResourceFactory.NAMESPACE),
            reference.getProperty(ENSResourceFactory.PATTERN),
            (Long) reference.getProperty(
                    it.txt.ens.client.publisher.osgi.FilterAttributes.PUBLISHER_OWNER_KEY));
    }

    /* (non-Javadoc)
     * @see org.osgi.framework.Filter#match(java.util.Dictionary)
     */
    @Override
    public boolean match(Dictionary<String, ?> dictionary) {
        return matches(dictionary.get(
                it.txt.ens.client.publisher.osgi.FilterAttributes.PUBLISHER_IMPLEMENTATION_KEY),
                dictionary.get(ENSResourceFactory.NAMESPACE),
                dictionary.get(ENSResourceFactory.PATTERN),
                (Long) dictionary.get(it.txt.ens.client.publisher.osgi.FilterAttributes.PUBLISHER_OWNER_KEY));
    }

    /* (non-Javadoc)
     * @see org.osgi.framework.Filter#matchCase(java.util.Dictionary)
     */
    @Override
    public boolean matchCase(Dictionary<String, ?> dictionary) {
        return matches(dictionary.get(
                it.txt.ens.client.publisher.osgi.FilterAttributes.PUBLISHER_IMPLEMENTATION_KEY),
                dictionary.get(ENSResourceFactory.NAMESPACE),
                dictionary.get(ENSResourceFactory.PATTERN),
                (Long) dictionary.get(it.txt.ens.client.publisher.osgi.FilterAttributes.PUBLISHER_OWNER_KEY));
    }

    /* (non-Javadoc)
     * @see org.osgi.framework.Filter#matches(java.util.Map)
     */
    @Override
    public boolean matches(Map<String, ?> map) {
        return matches(map.get(
                it.txt.ens.client.publisher.osgi.FilterAttributes.PUBLISHER_IMPLEMENTATION_KEY),
                map.get(ENSResourceFactory.NAMESPACE),
                map.get(ENSResourceFactory.PATTERN),
                (Long) map.get(it.txt.ens.client.publisher.osgi.FilterAttributes.PUBLISHER_OWNER_KEY));
    }
    
    private boolean matches (Object thatImplementationID, Object thatNamespace, Object thatPattern, long ownerID) {
        System.out.println(thatImplementationID + "-----" + thatNamespace + "-----" + thatPattern + "-----" + ownerID);
        
        return  unescapedImplementationID.equals(thatImplementationID) &&
                unescapedNamespace.equals(thatNamespace) &&
                unescapedPattern.equals(thatPattern) &&
                bundleOwnerID == ownerID;
    }

    
    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (obj instanceof PublisherFilter) {
            PublisherFilter that = (PublisherFilter) obj;
            return this.bundleOwnerID == that.bundleOwnerID && 
                this.unescapedImplementationID.equals(that.unescapedImplementationID) &&
                this.unescapedNamespace.equals(that.unescapedNamespace) &&
                this.unescapedPattern.equals(that.unescapedPattern);
        }
        return false;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return encodedOsgiFilter.hashCode();
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        System.out.println("Publisher filter = " + encodedOsgiFilter);
        
        return encodedOsgiFilter;
    }

    /**
     * @return the unescapedImplementationID
     */
//    public String getUnescapedImplementationID() {
//        return unescapedImplementationID;
//    }

    /**
     * @param unescapedImplementationID the unescapedImplementationID to set
     */
//    public void setUnescapedImplementationID(String unescapedImplementationID) {
//        this.unescapedImplementationID = unescapedImplementationID;
//    }

    /**
     * @return the unescapedPattern
     */
//    public String getUnescapedPattern() {
//        return unescapedPattern;
//    }

    /**
     * @param unescapedPattern the unescapedPattern to set
     */
//    public void setUnescapedPattern(String unescapedPattern) {
//        this.unescapedPattern = unescapedPattern;
//    }

    /**
     * @return the unescapedNamespace
     */
//    public String getUnescapedNamespace() {
//        return unescapedNamespace;
//    }

    /**
     * @param unescapedNamespace the unescapedNamespace to set
     */
//    public void setUnescapedNamespace(String unescapedNamespace) {
//        this.unescapedNamespace = unescapedNamespace;
//    }

    /**
     * @return the bundleOwnerID
     */
//    public long getBundleOwnerID() {
//        return bundleOwnerID;
//    }

    /**
     * @param bundleOwnerID the bundleOwnerID to set
     */
//    public void setBundleOwnerID(long bundleOwnerID) {
//        this.bundleOwnerID = bundleOwnerID;
//    }
}
